package saucaca.fragment;

import java.util.ArrayList;

import saucaca.CustomView.PagerSlidingTabStrip;
import saucaca.Object.HomeData;
import saucaca.Object.TelcoObject;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.lemon.kenhnhacvn.MainActivity;
import com.lemon.kenhnhacvn.R;


public class RingtoneFragment extends Fragment {
	private MainActivity mActivity = null;
	private PagerSlidingTabStrip tabs;
	ViewPager viewPager;
	RingToneAdapter TabAdapter;

	@SuppressLint("NewApi")
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//		mActivity.getActionBar().hide();
		View view = inflater.inflate(R.layout.viewpager_tabstrip, container, false);
		findViewById(view);
		mActivity.myActionBar.setMenu(R.string.menu_nhaccho, false, true);
		return view;
	}
	@Override
	public void onHiddenChanged(boolean hidden) {
		super.onHiddenChanged(hidden);
		if(!hidden)
			mActivity.myActionBar.setMenu(R.string.menu_nhaccho, false, true);
	}

	public void findViewById(View view) {
		viewPager = (ViewPager) view.findViewById(R.id.viewPager);
		tabs = (PagerSlidingTabStrip) view.findViewById(R.id.tabs);
		TabAdapter = new RingToneAdapter(getChildFragmentManager());
		final int pageMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2, getResources().getDisplayMetrics());
		viewPager.setPageMargin(pageMargin);
		viewPager.setAdapter(TabAdapter);
		int telco=((ArrayList<TelcoObject>) HomeData.homeListTelco).size();
		tabs.setViewPager(viewPager);
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		if (activity instanceof MainActivity) {
			mActivity = (MainActivity) activity;
		}
	}

	public class RingToneAdapter extends FragmentStatePagerAdapter {
		ArrayList<TelcoObject> listTelco;

		public RingToneAdapter(FragmentManager fm) {
			super(fm);
			listTelco = (ArrayList<TelcoObject>) HomeData.homeListTelco;
		}

		@Override
		public CharSequence getPageTitle(int position) {
			return listTelco.get(position).getTXNAME();
		}

		@Override
		public int getCount() {
			return listTelco.size();
		}

		@Override
		public Parcelable saveState() {
			return null;
		}

		@Override
		public Fragment getItem(int i) {
			return new RingToneListTone(listTelco.get(i));
		}
	}
}
